package com.nan.gbd.library.codec.recordcodec;

import com.nan.gbd.library.codec.configuration.Configuration;
import com.nan.gbd.library.sip.RecordBean;
import com.nan.gbd.library.sip.SipUaNative;
import com.nan.gbd.library.utils.Logger;
import com.nan.gbd.library.utils.SpRecordUtils;

import java.io.File;
import java.util.Map;

public class MediaRecordCaching {

    private static MediaRecordCaching mInstance;

    private RecordBean recordBean;

    public static synchronized MediaRecordCaching getInstance() {
        if (mInstance == null) {
            mInstance = new MediaRecordCaching();
        }
        return mInstance;
    }

    /**
     * 创建一条新的记录
     */
    public void createCaching() {
        Map<String, File> fileMap = Configuration.get().getGB28181EmptyVideoFile();
        if (recordBean == null)
            recordBean = new RecordBean();
        if (fileMap == null) return;
        File childFile = fileMap.get("file_child");
        String mPath = childFile.getAbsolutePath();
        recordBean.setFileName(childFile.getName());
        recordBean.setPath(mPath);

        File superFile = fileMap.get("file_super");
        recordBean.setAddress(superFile.getAbsolutePath());
        recordBean.setAddressName(superFile.getName());
    }


    /**
     * 将录制记录缓存本地
     */
    public void saveCaching(long startTime, long endTime, long mPackedFileSize) {
        if (recordBean == null) return;
        recordBean.setStartTime(startTime);
        recordBean.setEndTime(endTime);
        recordBean.setPackedFileSize(mPackedFileSize);
        SpRecordUtils.putVideoRecordKey(String.valueOf(endTime));
        SpRecordUtils.putObject(String.valueOf(endTime), recordBean);

        //录制完成将录制信息更新到sdk
        SipUaNative.putRecordInfo(recordBean.getStartTime(), recordBean.getEndTime(), recordBean.getAddress(), recordBean.getPath(), recordBean.getPackedFileSize());
        Logger.e("保存记录并将录制信息更新到sdk：" + recordBean.toString());
        destroy();
    }

    public RecordBean getRecord() {
        return recordBean;
    }

    private void destroy() {
        recordBean = null;
    }
}
